import { defineConfig } from 'vite'
import { resolve } from 'path'
import WindiCSS from 'vite-plugin-windicss'
import uni from '@dcloudio/vite-plugin-uni'
import { vueI18n } from '@intlify/vite-plugin-vue-i18n'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { apiHost } from './src/config'

export default defineConfig({
    plugins: [
        vueJsx(),
        WindiCSS(),
        uni(),
        vueI18n({
            include: resolve(__dirname, './src/locales/**'),
            defaultSFCLang: 'yml'
        })
    ],
    resolve: {
        alias: [{ find: '@', replacement: resolve(__dirname, 'src') }]
    },
    server: {
        proxy: {
            '/dev-api': {
                target: apiHost,
                changeOrigin: true,
                ws: true,
                secure: false,
                rewrite: path => path.replace(/^\/dev-api/, '')
            }
        }
    },
    build: {
        rollupOptions: {
            output: {
                manualChunks(id) {
                    if (id.includes('node_modules')) {
                        const names = id.split('node_modules/')
                        const lastPath = names[names.length - 1]
                        return '_lib_' + lastPath.split('/')[0].replace('@', '_')
                    }
                }
            }
        },
        terserOptions: {
          compress: {
            drop_debugger: true,
          }
        }
    }
})
